package com.jn.rxjava.views;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jn.rxjava.R;
import com.jn.rxjava.adapter.FoodAdapter;
import com.jn.rxjava.base.BaseActivity;
import com.jn.rxjava.contract.FoodContract;
import com.jn.rxjava.entity.FoodEntity;
import com.jn.rxjava.presenter.FoodPresenter;

import java.util.ArrayList;
import java.util.List;

public class FoodActivity extends BaseActivity implements
        FoodContract.IFoodView, View.OnClickListener {

    private FoodContract.IFoodPresenter mPresenter;
    private Button mButton;
    private EditText mEdit;
    private RecyclerView mRecyclerView;
    private FoodAdapter mAdapter;
    private List<FoodEntity.DataItem> mList;
    @Override
    public int getLayoutId() {
        return R.layout.activity_food;
    }

    @Override
    public void init() {
        mPresenter = new FoodPresenter(this, this);
        mButton = findViewById(R.id.button);
        mEdit = findViewById(R.id.edit);
        mRecyclerView = findViewById(R.id.recyclerView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mList = new ArrayList<>();
        mAdapter = new FoodAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        mButton.setOnClickListener(this);
    }

    @Override
    public void showData(FoodEntity data) {
        mList = data.getData();
        mAdapter.updateData(mList);
    }

    @Override
    public void onHttpLoading(boolean loading) {

    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.button) {
            String keyWord = mEdit.getText().toString();
            if (!TextUtils.isEmpty(keyWord)) {
                mPresenter.getData(keyWord);
            }
        }
    }
}